﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AutoMapper;
using WebStore.Domain.Entities.Model;
using WebStore.Web.App.Models.Account;

namespace WebStore.Web.App.Controllers
{
    public partial class AccountController
    {
        [Authorize]
        public ViewResult Profile()
        {
            var user = _entities.Users.FirstOrDefault(x => x.Email == HttpContext.User.Identity.Name);
            return View(Mapper.Map<ProfileModel>(user));
        }

        [HttpPost]
        [Authorize]
        [ActionName("Profile")]
        public ViewResult ProfilePost()
        {
            
            var user = _entities.Users.FirstOrDefault(x => x.Email == HttpContext.User.Identity.Name);
            var model = Mapper.Map<ProfileModel>(user);
            if (!TryUpdateModel(model) || !ModelState.IsValid)
                return View(model);
            _accountService.Update(
                 HttpContext.User.Identity.Name,
                 model.FirstName,
                 model.SecondName,
                 model.LastName,
                 model.AddressLine,
                 model.City,
                 model.PostalCode
                );
            return View(model);
        }
    }
}